package dawnland03.model.entity;

import dawnland03.model.utility.Utility;
import dawnland03.model.entity.attribute.PhysicalAttribute;
import dawnland03.model.entity.attribute.Position;
import dawnland03.model.team.Team;

import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;

/**
 * User: Petru Obreja (obrejap@yahoo.com)
 * Date: Jan 18, 2010
 * Time: 10:18:12 PM
 */
public class EntityFactory {
    private final static ThreadGroup threadGroup;
    private final static Map<Integer, Entity> entities;

    private final static Double defaultPhysicalAttributeValue = 1.0;

    static {
        threadGroup = new ThreadGroup("Entities");
        entities = new HashMap<Integer, Entity>();
    }

    public static Set<Entity> createGroupOfEntities(Team team, Set<Position> positions){
        Set<Entity> groupsEntities = new HashSet<Entity>();
        for(Position position:positions){
            Entity entity = createEntity(team, position);
            groupsEntities.add(entity);
        }
        return groupsEntities;
    }

    public static Entity createEntity(Team team, Position position) {
        Map<PhysicalAttribute, Double> physicalAttributes = new HashMap<PhysicalAttribute, Double>();
        for (PhysicalAttribute physicalAttribute : PhysicalAttribute.values()) {
            physicalAttributes.put(physicalAttribute, defaultPhysicalAttributeValue);
        }
        EntityImpl entity = new EntityImpl(Utility.getNextUuid(), position, physicalAttributes, team);
        EntityBrain entityBrain = new EntityBrain(entity);
        entity.setEntityBrain(entityBrain);
        entities.put(entity.getId(), entity);
        Thread thread = new Thread(threadGroup, entityBrain, "Entity "+entity.getId());
        thread.start();
        return entity;
    }
}
